<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/28
 * Time: 11:09
 */
namespace app\web\service;

use app\web\model\Shop;
use app\common\util\JwtUtil;
use think\facade\Log;

class ShopService{

    /**
     * @todo 获取当前店铺信息
     * @author ZJF
     */
    public function getShop($shop_id = 0,$shop_info = []){
        if(empty($shop_id) || !is_numeric($shop_id)){
            return array_response([],1,"无效的店铺ID");
        }
        if(empty($shop_info)){
            $shop_model = new Shop();
            $shop_info = $shop_model->where("id='{$shop_id}'")->find();
            $shop_info = ObjectToArray($shop_info);
        }
        $data = array(
            'shop_name'=>$shop_info['login_id'],
        );
        return $data;
    }

    /**
     * @todo 关联多店铺
     * @author ZJF
     */
    public function relationShop($shop_id = 0,$user=[],$login_id = ''){
        if(empty($shop_id) || !is_numeric($shop_id)){
            return array_response([],1,"无效的店铺ID");
        }
        if(empty($login_id)){
            return array_response([],1,"无效的登录账号");
        }
        //根据登录账号查询已订购店铺信息
        $shop_model = new Shop();
        $shop_info = $shop_model->where("login_id='{$login_id}' AND is_del=0 AND id<>{$shop_id}")->find();
        $shop_info = ObjectToArray($shop_info);
        if(empty($shop_info)){
            return array_response([],1,"无此登录账号信息");
        }
        //有订购，且，店铺没有关联其他店铺
        if($shop_info['ali_id']!=$shop_info['pid']){
            return array_response([],1,"该店铺已存在关联数据");
        }
        //更新关联ali_id
        $shop_data = array(
            'pid'=>$user['ali_id'],
            'mtime'=>date('Y-m-d H:i:s')
        );
        $shop_model->where("id='{$shop_id}'")->save($shop_data);
        return true;
    }

    /**
     * @todo 获取关联店铺列表
     * @author ZJF
     */
    public function getRelationShop($shop_id = 0,$user = []){
        if(empty($shop_id) || !is_numeric($shop_id)){
            return array_response([],1,"无效的店铺ID");
        }
        $shop_model = new Shop();
        $shop_list = $shop_model
                    ->field("id,login_id")
                    ->where("pid='{$user['pid']}' AND is_del=0 AND id<>{$shop_id}")
                    ->select();
        return ObjectToArray($shop_list);
    }

    /**
     * @todo 切换关联店铺
     * @author ZJF
     */
    public function switchShop($shop_id = 0,$user = [],$relation_shop_id = 0){
        if(empty($shop_id) || !is_numeric($shop_id)){
            return array_response([],1,"无效的店铺ID");
        }
        if(empty($relation_shop_id) || !is_numeric($relation_shop_id)){
            return array_response([],1,"无效的关联店铺ID");
        }
        //根据关联店铺ID，获取要切换到的店铺信息
        $shop_model = new Shop();
        $shop_info = $shop_model->where("id='{$relation_shop_id}' AND is_del=0 AND id<>{$shop_id}")->find();
        $shop_info = ObjectToArray($shop_info);
        if(empty($shop_info)){
            return array_response([],1,"无此关联店铺信息");
        }
        //比较店铺与当前店铺是否有关联关系
        if($user['pid']!=$shop_info['pid']){
            return array_response([],1,"请先关联店铺");
        }
        //返回鉴权token
        //生成令牌
        $payload['shop_id'] = $shop_info['id'];
        $token = think_encrypt(JwtUtil::encode($payload), '', 24*60*60);
        return $token;
    }
}